package com.byq.mm.common.engine;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;


public class ContactManager {

	Environment env;
	public ContactManager(Environment env) {
		super();
		this.env = env;
	}
	public Collection<Contact> getContacts(){
		List<Contact> rst = new ArrayList<Contact>();
		XMPPConnection con = env.getConnection();
		if(con == null)
			return rst;
		Collection<RosterEntry> es = Roster.getInstanceFor(con).getEntries();
		for(RosterEntry e : es)
			rst.add(getContact(e));
		return rst;
	}
	public void delete(Contact c){
try {

	RosterEntry re = Roster.getInstanceFor(env.getConnection()).getEntry(c.getTransport());

	Roster.getInstanceFor(env.getConnection()).removeEntry(re);

}catch(Exception e){
	e.printStackTrace();
}
	}
	public void add(Contact c){


        try {
            Roster.getInstanceFor(env.getConnection()).createEntry(c.getTransport(), c.getName(), new String[]{""});
        } catch (SmackException.NotLoggedInException e) {
            throw new RuntimeException(e);
        } catch (SmackException.NoResponseException e) {
            throw new RuntimeException(e);
        } catch (XMPPException.XMPPErrorException e) {
            throw new RuntimeException(e);
        } catch (SmackException.NotConnectedException e) {
            throw new RuntimeException(e);
        }

    }
	public Contact getContact(String jid){
		XMPPConnection con = env.getConnection();
		if(con == null)
			return null;

        RosterEntry e = null;

            e = Roster.getInstanceFor(con).getEntry(jid);

        if(e == null)
			return null;
		return getContact(e);
	}
	Contact getContact(RosterEntry re){
		Contact c = new Contact();
		c.setTransport(re.getUser());
		c.setName(re.getName());
        Presence p =  Roster.getInstanceFor(env.getConnection()).getPresence(re.getUser());


        switch(p.getType()){
		case available:
			c.setType(Contact.Type.available);
			c.setOnline(true);
			break;
		case unavailable:
			c.setType(Contact.Type.unavailable);
			break;
		case subscribe:
			c.setType(Contact.Type.subscribe);
			break;
		case unsubscribe:
			c.setType(Contact.Type.unsubscribe);
			break;
		case subscribed:
			c.setType(Contact.Type.subscribed);
			break;
		case unsubscribed:
			c.setType(Contact.Type.unsubscribed);

			
		}
		if(p.getMode() == null)
			return c;
		switch(p.getMode()){
		case available:
			c.setMode(Contact.Mode.available);
			break;
		case away:
			c.setMode(Contact.Mode.away);
			break;
		case chat:
			c.setMode(Contact.Mode.chat);
			break;
		case dnd:
			c.setMode(Contact.Mode.dnd);
			break;
		case xa:
			c.setMode(Contact.Mode.xa);


			
		}
		return c;
	}
	public void agree(String to){
        Presence p = null;

            p = new Presence(Presence.Type.subscribed);
			p.setTo(to);


        try {
            env.getConnection().sendStanza(p);
        } catch (SmackException.NotConnectedException e) {
            throw new RuntimeException(e);
        }

    }
	public void register(String userName,String password){

		
	}
	public void nickName(String jid, String nick){
        RosterEntry entry = null;
        try {
            entry = Roster.getInstanceFor(env.getConnection()).getEntry(jid);
			entry.setName(nick);
        }catch (XMPPException.XMPPErrorException e) {
            throw new RuntimeException(e);
        } catch (SmackException.NotConnectedException e) {
            throw new RuntimeException(e);
        } catch (SmackException.NoResponseException e) {
            throw new RuntimeException(e);
        }

    }
}
